<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .body-style{
            width: 500px;
            margin: auto;
        }
        .box-flex{

            width: 50%;
            display: inline-block;
        }
        .cpn-style{
            margin: 0 2%;
            margin-top: 30px;
            outline: 1px solid #9a9a9a;
            border-radius: 10px;
            overflow: hidden;
            background-color: #efefef;
        }
        .cpn-img{
            width: 100%;
        }
        img{
            width: 100%;
        }
        .pirce{
            font-size: 24px;
            text-align: center;
            color: rgb(255, 77, 0);
        }
        .text{
            color: rgb(0, 0, 0);
            text-align: center;
        }
        h2{
            text-align: center;
        }


    </style>
</head>
<body>
    <!-- vue实例 -->
    <div id="app" class="body-style">
        <h2>您当前点击的是 : {{ msg }} </h2>
        <div class="box-flex" v-for="item in list" :key="item.id">
            <my-cpn :listdata="item" @changeid="changeIdData"></my-cpn>
        </div>
    </div>

    <!-- 组件 -->
    <template id="myCpn">
        <div class="cpn-style" @click="clickCpn">
            <div class="cpn-img">
                <img :src="listdata.imgUrl" alt="">
            </div>
            <div class="text">
                {{ listdata.text }}
            </div>
            <div class="pirce">
                {{ listdata.price | newPrice }}
            </div>
        </div>
    </template>
    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el:'#app',
            data(){
                return {
                    list:[
                        {
                            id:1,
                            imgUrl:"./img/0.png",
                            text:"我是第1个数组中的内容吧",
                            price:999
                        },
                        {
                            id:2,
                            imgUrl:"./img/2.png",
                            text:"我是第2个数组中的内容吧",
                            price:999
                        },
                        {
                            id:3,
                            imgUrl:"./img/3.png",
                            text:"我是第3个数组中的内容吧",
                            price:999
                        },
                        {
                            id:4,
                            imgUrl:"./img/4.png",
                            text:"我是第4数组中的内容吧",
                            price:999
                        },
                        {
                            id:5,
                            imgUrl:"./img/5.png",
                            text:"我是第5个数组中的内容吧",
                            price:999
                        },
                        {
                            id:6,
                            imgUrl:"./img/6.png",
                            text:"我是第6个数组中的内容吧",
                            price:999
                        },
                        {
                            id:7,
                            imgUrl:"./img/7.png",
                            text:"我是第7个数组中的内容吧",
                            price:999
                        },
                        {
                            id:8,
                            imgUrl:"./img/1.png",
                            text:"我是第8个数组中的内容吧",
                            price:999
                        },
                    ],
                    msg:null
                }
            },
            methods:{
                changeIdData(val){
                    if(val === 1){
                        this.msg = '第一个'
                    }else if(val === 2){
                        this.msg = '第二个'
                    }else if(val === 3){
                        this.msg = '第三个'
                    }else if(val === 4){
                        this.msg = '第四个'
                    }else if(val === 5){
                        this.msg = '第五个'
                    }else if(val === 6){
                        this.msg = '第六个'
                    }else if(val === 7){
                        this.msg = '第七个'
                    }else{
                        this.msg = '第八个'
                    }
                }
            },
            components:{
                'my-cpn':{
                    template:'#myCpn',
                    props:['listdata'],
                    data(){
                        return {

                        }
                    },
                    methods:{
                        clickCpn(){
                            this.$emit('changeid',this.listdata.id)
                        }
                    },
                    filters:{
                        newPrice(val){
                            return '¥' + val.toFixed(2)
                        }
                    }
                }
            }
        })
    </script>
</body>
</html>